<template>
  <view>
    <view class="search">
      <uni-search-bar  @input="input" :cancelButton="'none'" :radius="15" ></uni-search-bar>
    </view>
    <!-- 搜索列表 -->
    <view class="search_list">
      <view v-for="(item,index) in searchlist" :key="index" class="search_list_item" @click="gotodetail(item)">
        <view class="search_name">{{item.goods_name}}</view>
        <uni-icons type="right" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="search_histroy" v-if="searchlist.length===0">
      <view class="clear_histroy">
        <text>历史记录</text>
        <uni-icons type="trash" size="20" @click="clean"></uni-icons>
      </view>
      <view class="show_histroy">
        <uni-tag :text="item" v-for="(item,i) in shistroylist" :key="i" @click="gotogoodlist(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        sd:null,
        tx:'',
        searchlist:[],
        shistroylist:[]
        
      };
    },
    onLoad() {
      this.shistroylist=JSON.parse(uni.getStorageSync('aa')||'[]')
      
    },
    methods:{
      input(e){
        clearTimeout(this.sd)
        this.sd=setTimeout(()=>{
        if(e.length===0){
          this.searchlist=[]
        }
        else{
          uni.$getlistdata([uni.url_t.searchq,{query:e}],'searchlist',this).then((res)=>{
            if(res.length !=0){
              this.addshistroylist(e)
            }
          })
        }
        },500)
        //setTimeou返回值是立即返回的所以当在500毫秒内又执行input的话就会取消该定时器 所以也不会执行这个返回值回调函数
      },
      gotodetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      //获取历史数据 indexof是查找数据并返回索引
      //如果没找到返回-1
      //unshift向数组首位添加一位元素
      //setStorageSync是把数据存到本地 第一个参数是key值 第二个是字符串
      //JSON.stringify是把数组转化为字符串
      //JSON.parse将字符串转化为数组
      addshistroylist(a){
        if(this.shistroylist.indexOf(a)===-1&&a!=''){
          this.shistroylist.unshift(a)
          uni.setStorageSync('aa',JSON.stringify(this.shistroylist))
        } 
        
      },
      //删除历史数据
      clean(){
        this.shistroylist=[]
        uni.setStorageSync('aa','[]')
      },
      //跳转商品列表
      gotogoodlist(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+item
        })
      }
      
    }
  }
</script>

<style lang="scss">
  .search{
    position: sticky;
    top:0;
  }
  .search_list{
    padding: 0 15px;
    .search_list_item{
      display: flex;
      justify-content: space-between;
      padding:10px 0;
      border: 1px solid #f7f7f7;
      font-size: 12px;
      //align-items: center;
      .search_name{
        //文本不允许换行 这里要用行级元素
        white-space: nowrap;
        //超过的部分隐藏
        overflow: hidden;
        //超过的部分用省略号表示
        text-overflow: ellipsis;
        margin-right: 5px;
      }
    }
  }
  .search_histroy{
    padding: 0 10px;
    .clear_histroy{
      height: 30px;
      border: 1px solid #f7f7f7;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 5px;
    }
  }
  .show_histroy{
    display: flex;
    flex-wrap: wrap;
    .uni-tag{
      background-color: #f7f7f7;
      margin-right: 5px;
    }
  }

</style>
